//
//  SQLiteTableiabe.swift
//  MYModule
//
//  Created by 尹琼 on 2022/11/5.
//

import UIKit
import SQLite
import SQLiteValueExtension
import HandyJSON


public protocol SQLiteTableiabe: NSObject {
    
    /// connection
    static var db: Connection { get }
    
    /// 表对象
    static var table: Table { get }
    
    /// 表名
    static var tableName: String { get }
    
    /// 建表 sql 语句
    static var create: String { get }
    
    /// 表结构升级
    static func updateColumns()
    
    /// 数据库Row对象转字典
    static func dict(row: Row) ->[String: Any]
    
    /// 数据库Statement对象转字典数组
    static func dictArray(statement: Statement) ->[[String: Any]]
    
    
    /// id  自增字段
    var id: Int64 { get set }
    
    /// 数据存储日期
    var date: Date { get set }
    

}

// MARK: - 基础方法
public extension SQLiteTableiabe {
    
    static var db: Connection {
        
        return SQLiteManager.shared.connection!
    }
    
    static var table: Table {
        return Table(tableName)
    }
    
    static var tableName: String {
        return "\(Self.classForCoder())"
    }
    
    static func hasColumns(_ column :String) -> Bool {
            do {
                var columnDatas :[String] = []
                
                let s = try Self.db.prepare("PRAGMA table_info(\(self.tableName))" )
                for row in s { columnDatas.append(row[1]! as! String) }
                let list = columnDatas.filter { (item) -> Bool in
                    return item == column
                }
                return list.count > 0
            }
            catch {
                return false
            }
        }
    
    
    static var expression_id: Expression<Int64> {
        
        return Expression<Int64>("id")
        
    }
    
    static var expression_date: Expression<Date> {
        
        return Expression<Date>("date")
        
    }
    
    
    static var count: Int {
        
        do {
            
            return try db.scalar(table.count)
            
        } catch { return 0 }
        
    }
    
}

// MARK: - 实例方法
public extension SQLiteTableiabe {
    
    var db: Connection {
        return Self.db
    }
    
    var table: Table {
        return Self.table
    }
    
    var tableName: String {
        return Self.tableName
    }
    
    /// 建表 sql 语句
    var create: String {
        return Self.create
    }
    
    
    var expression_id: Expression<Int64> {
        
        return Self.expression_id
        
    }
    
    var expression_date: Expression<Date> {
        
        return Self.expression_date
        
    }
    
   

}


extension SQLiteTableiabe {
    
    static func == (lhs: Self, rhs: Self) -> Bool {
        
        return lhs.id == rhs.id
    }
    
    public static func != (lhs: Self, rhs: Self) -> Bool {
        return lhs.id != rhs.id
    }
    
}


public extension Optional {
    
    func expression(_ name: String) -> Expression<Wrapped> {
        return Expression<Wrapped>(name)
    }
    
}

public extension Value {
    
    func expression(_ name: String) -> Expression<Self> {
        return Expression<Self>(name)
    }
}




